import axios from 'axios'

//引入nprogress进度条
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

export function request(config) {
    // 1.创建axios的实例
    const instance = axios.create({
        baseURL: 'http://124.223.60.155:8889/api/private/v1/',
        timeout: 5000
    })

    // 2.axios的拦截器
    // 2.1.请求拦截的作用
    instance.interceptors.request.use(config => {
        //nprogress进度条开始
        nprogress.start();
        config.headers.Authorization = window.sessionStorage.getItem('token');
        return config
    }, err => {
        // console.log(err);
    })

    // 2.2.响应拦截
    instance.interceptors.response.use(res => {
        //进度条结束
        nprogress.done()
        return res.data
    }, err => {
        console.log(err);
    })

    // 3.发送真正的网络请求
    return instance(config)
}